<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" @submit.native.prevent>
      <el-form-item label="类型名称">
        <el-input size="small" v-model="listQuery.q_name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
        <el-button icon="el-icon-refresh-left" size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button size="small" type="primary" @click="addBrand" v-permission="['admin/product/type/create']">添加</el-button>
      <el-button size="small" type="primary" @click="editBrand" v-permission="['admin/product/type/update']">修改</el-button>
      <el-button size="small" type="primary" @click="deleteType" v-permission="['admin/product/type/del']">删除</el-button>
    </div>
    <el-table
            ref="singleTable"
            :data="dataList"
            v-loading="listLoading"
            border
            fit
            highlight-current-row
            style="width: 100%"
            @current-change="handleCurrentChange"
    >
      <el-table-column
              type="index"
              align="center"
              width="50">
      </el-table-column>
      <el-table-column
              property="name"
              label="类型名称"
              width="100">
      </el-table-column>
      <el-table-column
              property="normNames"
              label="关联规格"
              width="100">
      </el-table-column>
      <el-table-column
              show-overflow-tooltip
              property="brandNames"
              label="关联品牌">
      </el-table-column>
      <el-table-column
              property="sort"
              label="权重"
              width="60">
      </el-table-column>
      <el-table-column
              property="createUser"
              label="创建人"
              width="100">
      </el-table-column>
      <el-table-column
              property="createTime"
              label="创建时间"
              width="160">
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>
<script>
  import { typeList, deleteType } from '@/api/product'
  import { getCodeManager } from '@/api/all'
  import Pagination from '@/components/Pagination'
  import { Message } from 'element-ui'
  export default {
    name: 'Type',
    components: { Pagination },
    data() {
      return {
        dataList: [],
        checkBrandName: '',
        total: 0,
        listLoading: false,
        listQuery: {
          page: 1,
          limit: 20,
          q_name: ''
        },
        currentRow: null
      }
    },
    created() {
      getCodeManager('BRAND_TOP', () => {})
      this.getList()
    },
    methods: {
      handleCurrentChange(val) {
        // 选中当前行数据
        this.currentRow = val
      },
      // 获取列表数据
      getList() {
        this.listLoading = true
        typeList(this.listQuery).then(response => {
          this.dataList = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      reset() {
        this.listQuery.q_name = ''
        this.getList()
      },
      addBrand() {
        this.$router.push({ path: '/product/typeadd' })
      },
      editBrand() {
        if(!this.currentRow) {
          Message.error('请选择操作行。')
          return;
        };
        this.$router.push({path: '/product/typeedit',query:{id:this.currentRow.id}});
      },
      deleteType() {
        if(!this.currentRow) {
          Message.error('请选择操作行。')
          return;
        };
        this.$confirm('确定删除吗？', '提示', {
          onfirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteType({id: this.currentRow.id}).then(res => {
            if(res.data.success) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getList()
            } else {
              this.$message({
                type: 'error',
                message: res.data.message
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消操作'
          });
        });
      }
    }
  }
</script>
<style scoped>
  .filter-container .el-col-6 {
    margin-bottom: 15px;
  }
  .filter-item {
    margin-bottom: 0
  }
  /deep/ .filter-container label {
    font-size: 14px;
    font-weight: 500;
    margin-right: 10px;
  }
</style>
